<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
class CategoryController extends Controller {
    public function lst(){
        $cate = D('category');
        $cateres = $cate->order('cate_id asc')->catetree();
        $this->assign('cateres',$cateres);
        $this->display();
    }

    
    public function add(){
        $cate=D('category');
        if (IS_POST) {
            $data['cate_name'] = I('cate_name');
            $data['cate_ename'] = I('cate_ename');
            $data['cate_keywords'] = I('cate_keywords');
            $data['cate_desc'] = I('cate_desc');
            $data['cate_type'] = I('cate_type');
            $data['parentid'] = I('parentid');
            $data['cate_content'] = I('cate_content');
            if($_FILES['cate_pic']['tmp_name']!=''){
                $upload = new Upload(C('NEWUPLOAD'));// 实例化上传类
                $info   =   $upload->uploadOne($_FILES['cate_pic']);//单文件上传
                // $info   =   $upload->upload();//多文件上传
                if(!$info){
                    $this->error($upload->getError());// 上传错误提示错误信息
                }else{
                   $data['cate_pic']=$info['savepath'].$info['savename'];// 上传成功 获取上传文件信息
                }
            }
            if($cate->create($data)){
                if($cate->add()){
                    $this->success('添加信息成功！',U('lst'));
                }else{
                    $this->error('添加信息失败！');
                }
            }else{
                $this->error($cate->getError());
            }

            return;
        }
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
        $this->display();
    }


    public function edit($cate_id){
        $cate = D('category');
        if (IS_POST) {
            $data['cate_id'] = I('cate_id');
            $data['cate_name'] = I('cate_name');
            $data['cate_ename'] = I('cate_ename');
            $data['cate_keywords'] = I('cate_keywords');
            $data['cate_desc'] = I('cate_desc');
            $data['cate_type'] = I('cate_type');
            $data['parentid'] = I('parentid');
            $data['cate_content'] = I('cate_content');
            if($_FILES['cate_pic']['tmp_name']!=''){
                $upload = new Upload(C('NEWUPLOAD'));// 实例化上传类
                $info   =   $upload->uploadOne($_FILES['cate_pic']);//单文件上传
                // $info   =   $upload->upload();//多文件上传
                if(!$info){
                    $this->error($upload->getError());// 上传错误提示错误信息
                }else{
                   $data['cate_pic']=$info['savepath'].$info['savename'];// 上传成功 获取上传文件信息
                }
            }
            if($cate->create($data)){
                if($cate->save()){
                    $this->success('修改信息成功！',U('lst'));
                }else{
                    $this->error('修改信息失败！');
                }
            }else{
                $this->error($cate->getError());
            }

            return;
        }        
        $cateresa=$cate->find($cate_id);
        $this->assign('cateresa',$cateresa);
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
        $this->display();
    }

//删除
    public function del($cate_id){
        $cate = D('category');
        if($cate->delete($cate_id)){
            $this->success('删除成功',U('lst'));
        }else{
            $this->error('删除失败！');
        }
    }

//批量删除
    public function bdel () {
        $bdel = I('bdel');//获取的是数组
        if ($bdel) {
            $delid = implode(',', $bdel);//将数组输出为字符串,$delid则为被选中删除的所有栏目id
            // echo $delid;
            // die();
            $cate = D('category');
            if($cate->delete($delid)){
                $this->success('批量删除栏目成功',U('lst'));
            }else{
                $this->error('批量删除栏目失败',U('lst'));
            }
        }
    }











}